package com.poisonh.unifiedauth.ui;


import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.hardware.fingerprint.FingerprintManager;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.utils.PreferencesUtils;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FingerDialogFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FingerDialogFragment extends DialogFragment implements FingerprintUiHelper.Callback {
    FingerprintUiHelper.FingerprintUiHelperBuilder mFingerprintUiHelperBuilder;
    private FingerprintUiHelper mFingerprintUiHelper;
    private FingerprintManager.CryptoObject mCryptoObject;


    public FingerDialogFragment() {
        // Required empty public constructor

    }

    public static FingerDialogFragment newInstance() {
        FingerDialogFragment fragment = new FingerDialogFragment();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);//
        setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Material_Light_Dialog);
        FingerprintManager manager = (FingerprintManager) getActivity().getSystemService(Context.FINGERPRINT_SERVICE);

        mFingerprintUiHelperBuilder = new FingerprintUiHelper.FingerprintUiHelperBuilder(getActivity(), manager);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        getDialog().setTitle("验证指纹");
        View view = inflater.inflate(R.layout.fingerprint_dialog_container, container, false);
        mFingerprintUiHelper = mFingerprintUiHelperBuilder.build(
                (ImageView) view.findViewById(R.id.fingerprint_icon),
                (TextView) view.findViewById(R.id.fingerprint_status), this);
        return view;
    }

    @Override
    public void onPause() {
        super.onPause();
        mFingerprintUiHelper.stopListening();
    }

    @Override
    public void onResume() {
        super.onResume();
        mFingerprintUiHelper.startListening(mCryptoObject);
    }

    @Override
    public void onAuthenticated() {
        Log.i(">>>", "授权成功!");
        dismiss();
        Intent mIntent = new Intent(getActivity(), MainActivity.class);
        mIntent.putExtra("PHONE", PreferencesUtils.getString(getActivity(), "PHONE"));
        startActivity(mIntent);
        getActivity().finish();
    }

    @Override
    public void onError() {

    }

    /**
     * 设置CryptObject对象,将用于FingerprintManager用于识别指纹
     */
    public void setCryptoObject(FingerprintManager.CryptoObject cryptoObject) {
        mCryptoObject = cryptoObject;

    }
}
